﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Password.Models
{
    /// <summary>
    /// 加密类
    /// </summary>
    internal class MyEncoder
    {
        /// <summary>
        /// 铭文
        /// </summary>
        public string PlainText { get; set; }

        /// <summary>
        /// 密钥
        /// </summary>
        public string KeyText { get; set; }

        /// <summary>
        /// 密文
        /// </summary>
        public string CypherText { get; set; }

        /// <summary>
        /// 加密动作
        /// </summary>
        public void Encode()
        {
            //todo 加密过程

            char[] plainArray = PlainText.ToCharArray();
            char[] keyArray = KeyText.ToCharArray();
            //创建密文的集合
            List<char> cypherList = new List<char>();
            for (int i = 0; i < plainArray.Length; i++)
            {
                int number = (Function(keyArray[i % KeyText.Length]) + Function(plainArray[i])) % 26;
                char ch = Function2(number);
                cypherList.Add(ch);

            }
            CypherText = new string(cypherList!.ToArray());

        }

        /// <summary>
        /// 返回指定字符在字母表中的序号，从0开始
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        private int Function(char ch)
        {

            return ch - 'a';
        }

        /// <summary>
        /// 返回编号对应的字母
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        private char Function2(int number)
        {
            return (char)('a' + number);
        }
    }
}
